package priv.mill.controller;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import priv.mill.controller.util.RetenObj;
import priv.mill.dao.mapper.UserInfoMapper;
import priv.mill.entry.Answer;
import priv.mill.server.AnswerService2;
import priv.mill.server.UserInfoService;
import priv.mill.util.TimeUtil;

@Controller
@RequestMapping("Answer2")
public class AnswerController2 {

	@Autowired
	private AnswerService2 AnswerService;
	@Autowired
	private UserInfoService userInfoService;
	private static final Logger logger = Logger.getLogger(AnswerController2.class);

	@RequestMapping("getAnswerById")
	@ResponseBody
	public RetenObj getAnswerById(@RequestParam("AnswerId") Integer AnswerId) {
		try {
			return warpData(AnswerService.getAnswerById(AnswerId), 200);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return warpData(null, 500);
		}
	}

	private RetenObj warpData(Object object, int i) {
		RetenObj retenObj = new RetenObj();
		retenObj.setData(object);
		retenObj.setStatus(i);
		return retenObj;
	}

	@RequestMapping("getAnswerByEntry")
	@ResponseBody
	public RetenObj getAnswerByEntry(Answer Answer) {
		try {
			return warpData(AnswerService.getAnswerByEntry(Answer), 200);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return warpData(null, 500);
		}
	}

	@RequestMapping("getAnswersByPage")
	@ResponseBody
	public RetenObj getAnswersByPage(Answer Answer) {
		try {
			return warpData(AnswerService.getAnswersByPage(Answer), 200);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return warpData(null, 500);
		}
	}

	@RequestMapping("updateAnswer")
	@ResponseBody
	public RetenObj updateAnswer(Answer Answer) {
		try {
			int result = AnswerService.updateAnswer(Answer);
			if (result > 0)
				return warpData(result, 200);
			else
				return warpData(result, 300);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return warpData(null, 500);
		}
	}
	
	@RequestMapping("insertAnswer2")
	public String insertAnswer(Answer Answer,RedirectAttributes attr) {
		try {
			//设置当前时间为创建答案的时间
			Answer.setACreateTime(TimeUtil.getTime());
			//设置当前时间为更新答案的时间
			Answer.setAUpdateTime(TimeUtil.getTime());
			//设置用户ID
			Answer.setUId(1);
			//设置答案是否被封禁
			Answer.setAnswerLock(0);
			//调用方法向数据库插入答案
			int result = AnswerService.insertAnswer(Answer);
			attr.addAttribute("qId", Answer.getQId());
			attr.addAttribute("pageNumber", 1);
			return "redirect:../Question2/getQuestionByEntry";
		} catch (Exception e) {
			logger.error(e.getMessage());
			return "error";
		}
	}

//	@RequestMapping("insertAnswer2")
//	@ResponseBody
//	public RetenObj insertAnswer(Answer Answer) {
//		try {
//			//设置当前时间为创建答案的时间
//			Answer.setACreateTime(TimeUtil.getTime());
//			//设置当前时间为更新答案的时间
//			Answer.setAUpdateTime(TimeUtil.getTime());
//			//设置用户ID
//			Answer.setUId(1);
//			//设置答案是否被封禁
//			Answer.setAnswerLock(0);
//			//获取用户信息
//			UserInfo userInfoById = userInfoMapper.getUserInfoById(1);
//			//调用方法向数据库插入答案
//			int result = AnswerService.insertAnswer(Answer);
//			Answer answer = new Answer();
//			answer.setQId(Answer.getQId());
//			answer.setPageSize(5);
//			List<Answer> answersByPage = AnswerService.getAnswersByPage(answer);
//			List<UserInfo> list=new ArrayList<UserInfo>();
//			for (Answer answer2 : answersByPage) {
//				UserInfo userInfo = userInfoMapper.getUserInfoById(answer2.getUId());
//				list.add(userInfo);
//			}
//			HashMap<String, Object> hashMap = new HashMap<String, Object>();
//			hashMap.put("answersByPage", answersByPage);
//			hashMap.put("userInfolist", list);
//			//若插入成功，则将Answer对象返回前台页面
//			if (result > 0)
//				return warpData(hashMap, 200);
//			else
//				return warpData(hashMap, 300);
//		} catch (Exception e) {
//			logger.error(e.getMessage());
//			return warpData(null, 500);
//		}
//	}

	@RequestMapping("deleteAnswer")
	
	@ResponseBody
	public RetenObj deleteAnswer(Integer AnswerId) {
		try {
			int result = AnswerService.deleteAnswer(AnswerId);
			if (result > 0)
				return warpData(result, 200);
			else
				return warpData(result, 300);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return warpData(null, 500);
		}

	}
}
